package dbPicasso::Database;

use strict;
use warnings;
use Data::Dumper;

use base qw(DBI);

=head1 Overloaded DBI::connect

Lazy-load the appropriate helper module and use it to connect to the database.

=cut


sub connect
{
	my $class = shift;
	my %cxinfo = @_;

	unless ($cxinfo{Driver})
	{
		$@ = "Must specify a driver!";
		return undef;
	}

	my $helperpkg = "dbPicasso::Database::" . $cxinfo{Driver};
	print "Helper module: $helperpkg\n";

	eval "require $helperpkg";

	if ($@)
	{
		print ("Unable to load the helper module for the database driver you selected: '$cxinfo{Driver}'\n");
		print ("$@\n");
		return undef;
	}

	my $dbhelper = $helperpkg . "::db";
	my $sthelper = $helperpkg . "::st";

	my $DSN = $dbhelper->_gen_DSN(%cxinfo) or return undef;

	# Set inheritance to the appropriate helper module.

	@dbPicasso::Database::db::ISA = ($dbhelper);
	@dbPicasso::Database::st::ISA = ($sthelper);

	# Now we can connect to the database.
	my $db = $class->SUPER::connect($DSN, $cxinfo{UserID}, $cxinfo{Password});

	if ($db)
	{
		$db->_post_connect(%cxinfo) or warn ("Post-connect callback failed. Some things may not work correctly. This is probably a code problem.");
	}

	return $db;
}

=head1 dbPicasso::Database::db and ::st package methods

These classes are here as inheritance bridges, i.e. they just provide 
the @ISA references to the appropriate helper modules. Look for
default methods in the dbPicasso::Database::Helper module, and
overloads in the driver-specific helper modules, which inherit from Helper.

=cut

package dbPicasso::Database::db;

use vars qw(@ISA);


package dbPicasso::Database::st;

use vars qw(@ISA);

1;

